AWS CLIとシェルスクリプト、いつ使う?活用できる場面とTips紹介 #devio2024 #cm_odyssey

AWS CLIとシェルスクリプト、いつ使う?活用できる場面とTips紹介 #devio2024 #cm_odyssey

Clock Icon2024.08.01

2024/07/31にクラスメソッドの大阪オフィスにて DevelopersIO 2024 OSAKA が実施されました。

参加いただいたみなさま、ありがとうございます!

https://classmethod.connpass.com/event/322915/

その中で 「AWS CLI と シェルスクリプト、いつ使う?活用できる場面とTips紹介」 というタイトルで登壇させていただきました。

登壇の内容はひとことで言うと 「AWS CLI の出力と併せて、色んなテキスト処理コマンドを活用すると捗るよ」 みたいな感じです。

使った資料を本ブログで公開します。 参考になれば幸いです。

スライド

補足: スライド内のコード(「適切な出力を選ぼう」章)

yaml: 雑に grep

list-lambda-function-names.sh
aws lambda list-functions --output yaml \
| grep FunctionName:

⬇︎出力サンプル

FunctionName: test-function
FunctionName: process-aaa-info
FunctionName: generate-pdf-report
FunctionName: xxx-stack-LambdaFunction-9Hfba2

json: jq と連携

list-aws-managed-iam-policy-names.sh
aws iam list-policies --scope AWS --output json \
| jq -r '.Policies[].PolicyName'

⬇︎出力サンプル

AdministratorAccess
PowerUserAccess
ReadOnlyAccess
AWSCloudFormationReadOnlyAccess
CloudFrontFullAccess
AWSCloudHSMFullAccess
# ...以下略

table: query してテーブル化 #1

table-iam-roles.sh
aws iam list-roles --output table \
  --query "Roles[].[RoleName, CreateDate]"

⬇︎出力サンプル

----------------------------------------------------
|                   ListRoles                      |
+--------------------+-----------------------------+
|  aaa-ec2-role      |  2024-05-12T01:56:15+00:00  |
|  bbb-lambda-role   |  2023-10-13T08:46:23+00:00  |
|  hhh-cost-readonly |  2023-10-13T08:44:48+00:00  |
# ...略
|  xxx               |  2023-11-15T06:20:18+00:00  |
+--------------------+-----------------------------+

table: query してテーブル化 #2

table-iam-roles-with-column-name.sh
aws iam list-roles --output table \
  --query "Roles[].{Name:RoleName, CreatedAt:CreateDate}"

⬇︎出力サンプル

---------------------------------------------------
|                   ListRoles                     |
+----------------------------+--------------------+
|         CreatedAt          |       Name         |
+----------------------------+--------------------+
|  2024-05-12T01:56:15+00:00 |  aaa-ec2-role      |
|  2023-10-13T08:46:23+00:00 |  bbb-lambda-role   |
|  2023-10-13T08:44:48+00:00 |  hhh-cost-readonly |
# ...略
|  2023-11-15T06:20:18+00:00 |  xxx               |
+----------------------------+--------------------+

table: query してテーブル化 → markdown テーブル化

mdtable-iam-roles-with-column-name.sh
aws iam list-roles --output table \
  --query "Roles[].{Name:RoleName, CreatedAt:CreateDate}" \
| sed -e 's/^+/|/' -e 's/+$/|/' -e 's/-+-/-|-/g' \
| tail -n +4 | head -n -1

⬇︎出力サンプル

|         CreatedAt          |       Name         |
|----------------------------|--------------------|
|  2024-05-12T01:56:15+00:00 |  aaa-ec2-role      |
|  2023-10-13T08:46:23+00:00 |  bbb-lambda-role   |
|  2023-10-13T08:44:48+00:00 |  hhh-cost-readonly |
# ...略
|  2023-11-15T06:20:18+00:00 |  xxx               |

text: 値のリスト化

list-regions.sh
aws ec2 describe-regions --output text \
  --query "Regions[].[RegionName]"

⬇︎出力サンプル

ap-south-1
eu-north-1
eu-west-3
# ...略
us-west-2

text: 値のリスト化 → 繰り返し処理 #1

do-something-per-region.sh
aws ec2 describe-regions --output text \
  --query "Regions[].[RegionName]" \
| while read region; do
    echo "DO SOMETHING in ${region}"
  done

⬇︎出力サンプル

DO SOMETHING in ap-south-1
DO SOMETHING in eu-north-1
DO SOMETHING in eu-west-3
# ...略
DO SOMETHING in us-west-2

text: 値のリスト化 → 繰り返し処理 #2

table-lambda-names-and-runtimes.sh
aws lambda list-functions --output text \
  --query "Functions[].[FunctionName,Runtime]" \
| while read func_name runtime; do
    echo "| ${func_name} | ${runtime} |"
  done

⬇︎出力サンプル

| test-function | python3.11 |
| process-aaa-info | python3.10 |
| generate-pdf-report | java21 |
| xxx-stack-LambdaFunction-9Hfba2 | nodejs18.x |

text: リソースベースポリシーを整形

bucket-policy-json.sh
aws s3api get-bucket-policy --output text \
  --bucket ${bucket_name} --query Policy \
| jq

⬇︎出力サンプル

{
  "Version": "2012-10-17",
  "Statement": [
    {
      "Effect": "Allow",
      "Principal": {
        "AWS": [
          "arn:aws:iam::111111111111:role/example"
  # ...略
}

補足: スライド内のコード(「テキスト操作を楽しもう」章)

head: 先頭行を見る

describe-vpcs-first-10-rows.sh
aws ec2 describe-vpcs --output yaml \
| head

⬇︎出力サンプル

Vpcs:
- CidrBlock: 10.0.0.0/16
  CidrBlockAssociationSet:
  - AssociationId: vpc-cidr-assoc-01sample
    CidrBlock: 10.0.0.0/16
    CidrBlockState:
      State: associated
  DhcpOptionsId: dopt-59sample
  InstanceTenancy: default
  IsDefault: false

grep: 特定項目 "以外" で繰り返し処理

do-something-outside-the-us-east-1.sh
aws ec2 describe-regions --output text \
  --query "Regions[].[RegionName]" \
| grep -v us-east-1 \
| while read region; do
    echo "DO SOMETHING in ${region}"
  done

⬇︎出力サンプル

DO SOMETHING in ap-south-1
DO SOMETHING in eu-north-1
DO SOMETHING in eu-west-3
DO SOMETHING in eu-west-2
DO SOMETHING in eu-west-1
DO SOMETHING in ap-northeast-3
DO SOMETHING in ap-northeast-2
DO SOMETHING in ap-northeast-1
DO SOMETHING in ca-central-1
DO SOMETHING in sa-east-1
DO SOMETHING in ap-southeast-1
DO SOMETHING in ap-southeast-2
DO SOMETHING in eu-central-1
DO SOMETHING in us-east-2
DO SOMETHING in us-west-1
DO SOMETHING in us-west-2

cut: ARNから要素を抽出

list-sns-topic-names.sh
aws sns list-topics --output text \
  --query "Topics[].[TopicArn]" \
| cut -d : -f 6

⬇︎出力サンプル

aaa-topic
bbb-topic
ccc-topic

tr: 文字を置換

account-ids-comma-separated.sh
aws organizations list-accounts --output text \
  --query "Accounts[].Id" \
| tr "\t" ","

⬇︎出力サンプル

111111111111,222222222222,333333333333,444444444444,555555555555

sort: 並び替える #1

lambda-functions-sorted-by-names.sh
aws lambda list-functions --output text \
  --query "Functions[].[FunctionName, Runtime]" \
| sort

⬇︎出力サンプル

aaa-function    python3.12
bbb-function    python3.12
ccc-function    python3.10
ddd-function    java21
eee-function    nodejs20.x
fff-function    nodejs18.x

sort: 並び替える #2

lambda-functions-sorted-by-runtimes.sh
aws lambda list-functions --output text \
  --query "Functions[].[FunctionName, Runtime]" \
| sort -t "\t" -k 2

⬇︎出力サンプル

ddd-function    java21
fff-function    nodejs18.x
eee-function    nodejs20.x
ccc-function    python3.10
aaa-function    python3.12
bbb-function    python3.12

uniq: 重複排除 + カウント

number-of-functions-per-runtime.sh
aws lambda list-functions --output text \
  --query "Functions[].[Runtime]" \
| sort | uniq -c

⬇︎出力サンプル

   1 java21
   1 nodejs18.x
   1 nodejs20.x
   1 python3.10
   2 python3.12

wc: 行数のカウント

count-aws-managed-iam-policy.sh
aws iam list-policies --output text \
  --scope AWS \
  --query "Policies[]" \
| wc

⬇︎出力サンプル

    1222   12220  232481

参考

https://docs.aws.amazon.com/ja_jp/cli/latest/userguide/cli-usage-output-format.html
https://docs.aws.amazon.com/ja_jp/cli/latest/userguide/cli-usage-alias.html
https://aws.amazon.com/jp/builders-flash/202306/handle-aws-cli/
https://www.oreilly.co.jp//books/9784814400485/

この記事をシェアする

facebook logohatena logotwitter logo

© Classmethod, Inc. All rights reserved.